package com.song.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

/**
 * 登录功能
 */
@Controller
public class LoginController {

    // 账号密码登录，验证账号密码成功：进入业务界面，验证失败：提示信息，留在登录页
    @RequestMapping("/user/login")
    public String login(@RequestParam("username") String username,
                        @RequestParam("password") String password,
                        Model model,
                        HttpSession session){
        if(username != null && password.equals("123456")){
            // 设置session
            session.setAttribute("loginuser", username);
            System.out.println(session.getAttribute("loginuser"));
            return "redirect:/main.html";
        } else {
            model.addAttribute("msg","用户名或者密码错误");
            return "index";
        }
    }

    @RequestMapping("/user/loginout")
    public String loginout(HttpSession session) {
        session.invalidate();  //session失效
        return "redirect:/index.html";
    }

}
